#include "Extractor.h"

bool Extractor::mergePatterns( Patterns::reverse_iterator pattern1, Patterns::reverse_iterator pattern2 ){
	bool isSubPattern = true;
	for(
		vector<PatternWord>::iterator word = pattern1->words.begin();
		word != pattern1->words.end();
		word++
	){
		if( isEqualWords(pattern1->words.begin(), pattern2->words.begin()) ){
			vector<PatternWord>::iterator patternWord1 = word + 1;
			vector<PatternWord>::iterator patternWord2 = pattern2->words.begin() + 1;

			bool isEqual;
			for(
				isEqual = true;
				isEqual && patternWord2 != pattern2->words.end() && patternWord1 != pattern1->words.end();
				patternWord2++
			){
				if( !isEqualWords(patternWord1, patternWord2) ){
					isEqual = false;
				}
				patternWord1++;
			}

			if( isEqual && patternWord2 == pattern2->words.end() ){
				return true;
			}
		}
	}
	return false;
}

bool Extractor::isEqualWords( vector<PatternWord>::iterator word1, vector<PatternWord>::iterator word2 ){
	if( word1->name != "" && word2->name != "" )
		return word1->name == word2->name;

	if( word1->semanticClass != "" && word1->semanticClass == word2->semanticClass )
		return true;

	for( unsigned int i = 0; i < min(word1->speechPart.size(),word2->speechPart.size()); i++ )
		if( word1->speechPart[i] == word2->speechPart[i] )
			return true;

	return false;
}

bool Extractor::isWordMatched( Words::iterator word, vector<PatternWord>::iterator patternWord ){
	if( word->connector != patternWord->connector )
		return false;

	if( patternWord->name == "" || word->name == patternWord->name )
		return true;

	return false;
}

bool Extractor::isPatternWordsMatched( vector<PatternWord>::iterator patternWord1, vector<PatternWord>::iterator patternWord2 ){
	return patternWord1->connector == patternWord2->connector && patternWord1->name == patternWord2->name && patternWord1->role == patternWord2->role;
}